package com.example.myapplication;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * crated by Administrator on 2021/6/3
 * 746608431@qq.com
 * 描述：TODO
 */
public class RetrofitUtils {
    private static final int TIMEOUT_CONNECTION = 60;
    private static BaseApiService baseApiService;


    public static BaseApiService getApiUrl() {
        if (baseApiService == null) {
            synchronized (RetrofitUtils.class) {
                if (baseApiService == null) {
                    baseApiService = new RetrofitUtils().getRetrofit();
                }
            }
        }
        return baseApiService;
    }

    private RetrofitUtils() {
    }

    public BaseApiService getRetrofit() {

        // 初始化Retrofit

        BaseApiService apiUrl = initRetrofit(initOkHttp()).create(BaseApiService.class);

        return apiUrl;

    }

    private OkHttpClient initOkHttp() {
        OkHttpClient.Builder httpClient = new OkHttpClient().newBuilder();

        if (BuildConfig.DEBUG) {


//            httpClient.addInterceptor(new LoggingInterceptor.Builder().setLevel(Level.BASIC).build());//添加打印拦截器

            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            httpClient.addInterceptor(loggingInterceptor);//添加打印拦截器

        }

        return

                httpClient

                        .addInterceptor(new CustomInterceptor())
                        .addInterceptor(new EncryptInterceptor())
                        .readTimeout(TIMEOUT_CONNECTION, TimeUnit.SECONDS)//设置读取超时时间

                        .connectTimeout(TIMEOUT_CONNECTION, TimeUnit.SECONDS)//设置请求超时时间

                        .writeTimeout(TIMEOUT_CONNECTION, TimeUnit.SECONDS)//设置写入超时时间

                        .build();

    }

    private Retrofit initRetrofit(OkHttpClient okHttpClient) {
        return new Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl(BuildConfig.BASE_URL)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();

    }

    private class CustomInterceptor implements Interceptor {

        @Override

        public Response intercept(@NotNull Chain chain) throws IOException {

            Request request = chain.request();

            Request.Builder builder = request.newBuilder();

            // 解决下载文件时，response.body().contentLength()返回-1

            builder.header("Accept-Encoding", "identity");
//            if (!StringUtils.isEmpty(MMKVUitls.getToken())) {
//                builder.header("token", MMKVUitls.getToken());
//            }


            Request.Builder requestBuilder = builder.method(request.method(), request.body());

            return chain.proceed(requestBuilder.build());

        }

    }
}
